
import 'package:json_annotation/json_annotation.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_date.dart';


//定义生成的文件
part 'x_bean_select_time.g.dart';

@JsonSerializable(explicitToJson: true) //当内部存在嵌套类时使用,不存在也可以使用
class XBeanSelectTime {

  int? year;
  int? month;
  int? day;
  String? hourMinSec;//当前的时分秒

  int? yearWeek;//周
  int? quarter; //季度

  String? customStartTime;
  String? customEndTime;
  String? customTimeType;

  String? type; //日,月,周,季度,自定义


  XBeanSelectTime({
    this.type,
    this.year,
    this.month,
    this.day,
    this.yearWeek,
    this.quarter,
    this.customTimeType,
    this.customStartTime,
    this.customEndTime,
    this.hourMinSec,
  }){
    //
    type ??= '日';
    customTimeType ??= '开始';

    //
    if(year==null){
      year=DateTime.now().year;
      month=DateTime.now().month;
      day=DateTime.now().day;
    }else{
      if(month==null){
        month=1;
        day=1;
      }else{
        if(month!<=0 || month!>12){
          month=1;
        }
        if(day==null){
          day=1;
        }else{
          if(day!<=0 || day!>31){
            day=1;
          }
          if(month==2 && day!>28){
            if(XDateUtils.isRunNian(year!)){
              day=29;
            }else{
              day=28;
            }
          }else if(day!>30 && !XDateUtils.isMonth31Days(month!)){
            day=30;
          }
        }
      }
    }

    if('周'==type){
      if(yearWeek==null || yearWeek!<=0 || yearWeek!>XDateUtils.getWeeksOfYear(year!)){
        yearWeek=1;
      }
      DateTime? start=XDateUtils.getYearWeekRanges(year!, yearWeek!)['start'];
      month=start?.month;
      day=start?.day;

    }else if('季度'==type || '季'==type){
      if(quarter==null || quarter!<=0 || quarter!>4){
        quarter=1;
      }
      DateTime? start=XDateUtils.getQuarterRange(year!, quarter!)['start'];
      month=start?.month;
      day=start?.day;
    }

    //日,月,周,自定义
    DateTime newDate=DateTime(year!,month!,day!);
    yearWeek=XDateUtils.getYearWeekNumber(date: newDate)['yearWeek'];
    quarter=XDateUtils.getQuarterNumber(date: newDate);

    if('日'==type){
      customStartTime=XDateUtils.getStringTimeFormat(dateTime:newDate,format:XDateUtils.formatTimeDefault3);
      customEndTime=XDateUtils.getStringTimeFormat(dateTime:newDate,format:XDateUtils.formatTimeDefault3);
    }else if('周'==type){
      Map<String,DateTime> range=XDateUtils.getYearWeekRanges(year!, yearWeek!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);
    }else if('月'==type){
      DateTime? start=DateTime(year!,month!,1);
      DateTime? end=DateTime(year!,month!,XDateUtils.getDaysOfMonth(year!,month!));
      customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);
    }else if('季度'==type || '季'==type){
      Map<String,DateTime> range=XDateUtils.getQuarterRange(year!, quarter!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);
    }else if('自定义'==type){
      customStartTime??=XDateUtils.getStringTimeFormat(dateTime:newDate,format:XDateUtils.formatTimeDefault3);
      customEndTime??=XDateUtils.getStringTimeFormat(dateTime:newDate,format:XDateUtils.formatTimeDefault3);
    }


  }


  //添加固定格式: _$自定义的类名FromJson  _$自定义的类名ToJson
  factory XBeanSelectTime.fromJson(Map<String, dynamic> json) => _$XBeanSelectTimeFromJson(json);
  Map<String, dynamic> toJson() => _$XBeanSelectTimeToJson(this);


}
